﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using System.Drawing;
using System.Drawing.Imaging;

namespace ITOMMS.Web
{
    public partial class ValidateCode :System.Web.UI.Page
    {
        // 验证码长度
        private int codeLen = 4;
        // 图片清晰度
        private int fineness = 95;
        // 图片宽度
        private int imgWidth = 78;
        // 图片高度
        private int imgHeight = 25;
        // 字体家族名称
        private string fontFamily = "Times New Roman";
        // 字体大小
        private int fontSize = 18;
        // 字体样式
        private int fontStyle = 1;
        // 绘制起始坐标 X
        private int posX = 0;
        // 绘制起始坐标 Y
        private int posY = 0;
        private void Page_Load(object sender, System.EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                string validateCode = CreateValidateCode();

                // 生成BITMAP图像
                Bitmap bitmap = new Bitmap(imgWidth, imgHeight);

                // 给图像设置干扰
                DisturbBitmap(bitmap);

                // 绘制验证码图像
                DrawValidateCode(bitmap, validateCode);

                // 保存验证码图像，等待输出
                bitmap.Save(Response.OutputStream, ImageFormat.Gif);
            }
        }

        //------------------------------------------------------------
        // 随机生成验证码，并保存到SESSION中
        //------------------------------------------------------------
        private string CreateValidateCode()
        {
            string validateCode = "";
            // 随机数对象
            Random random = new Random();
            for (int i = 0; i < codeLen; i++)
            {
                // 26: a - z
                int n = random.Next(26);
                // 将数字转换成大写字母
                validateCode += (char)(n + 65);
            }
            // 保存验证码
            Session["ValidateCode"] = validateCode;
            return validateCode;
        }
        //------------------------------------------------------------
        // 为图片设置干扰点
        //------------------------------------------------------------
        private void DisturbBitmap(Bitmap bitmap)
        {
            // 通过随机数生成
            Random random = new Random();
            for (int i = 0; i < bitmap.Width; i++)
            {
                for (int j = 0; j < bitmap.Height; j++)
                {
                    if (random.Next(100) <= this.fineness)
                        bitmap.SetPixel(i, j, Color.White);
                }
            }
        }

        //------------------------------------------------------------
        // 绘制验证码图片
        //------------------------------------------------------------
        private void DrawValidateCode(Bitmap bitmap, string validateCode)
        {
            // 获取绘制器对象
            Graphics g = Graphics.FromImage(bitmap);
            // 设置绘制字体
            Font font = new Font(fontFamily, fontSize, GetFontStyle());
            // 绘制验证码图像
            g.DrawString(validateCode, font, Brushes.Red, posX, posY);
        }
        //------------------------------------------------------------
        // 换算验证码字体样式：1 粗体 2 斜体 3 粗斜体，默认为普通字体
        //------------------------------------------------------------
        private FontStyle GetFontStyle()
        {
            if (fontStyle == 1)
                return FontStyle.Bold;
            else if (fontStyle == 2)
                return FontStyle.Italic;
            else if (fontStyle == 3)
                return FontStyle.Bold | FontStyle.Italic;
            else
                return FontStyle.Regular;
        }
    }
}
